package view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListSelectionModel;

import matrix_operations.Matrix;

import basics.FileManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import javax.swing.ScrollPaneConstants;

import decomposition.Cholesky;
import decomposition.Crout;
import decomposition.DecompositionLDLT;
import decomposition.DecompositionLU;
import decomposition.DecompositionLUP;
import direct_methods_for_systems_solver.GaussElimination;
import javax.swing.JSpinner;

public class Janela {

	private JFrame frmMatrixOperations;
	private JTable tableSolution;
	private JPanel panel;
	private JPanel panel_2;
	private JButton buttonLoadI;
	private JButton buttonSaveI;
	private JScrollPane scrollPane;
	private JPanel panel_3;
	private JButton buttonLoadC;
	private JButton buttonSaveC;
	private JLabel lblNewLabel;
	private JComboBox<String> comboBoxSystem;
	private JPanel panel_1;
	private JButton buttonSaveL;
	private JScrollPane scrollPane_3;
	private JPanel panel_6;
	private JButton buttonSaveU;
	private JScrollPane scrollPane_4;
	private JPanel panel_7;
	private JButton buttonSaveO;
	private JTable tableCoefficients;
	private JTable tableIndendents;
	private JTable tableF1;
	private JTable tableF3;
	private JPanel panel_8;
	private JButton buttonLoadM;
	private JButton buttonSaveM;
	private JScrollPane scrollPane_6;
	private JButton btnDecompite;
	private JButton btnSolve;
	private JTable tableM;
	private JFileChooser jFChosen;
	private JComboBox comboBox_1;
	private JPanel panel_5;
	

	
	private Matrix A;
	private Matrix B;
	private Matrix S;
	private Matrix M;
	private Matrix F1;
	private Matrix F2;
	private Matrix F3;
	private JTable tableF2;
	private JButton btnNew;
	private JButton button;
	
	private void solveSystem(){
		
		A = tableToMatrix(tableCoefficients);
		B = tableToMatrix(tableIndendents);
		
		
		switch (comboBoxSystem.getSelectedIndex()){
			
			case 0:{
				S = GaussElimination.solveSystem(A, B);
				if (S == null){
					JOptionPane.showMessageDialog(frmMatrixOperations, GaussElimination.getMsg(), "Error", JOptionPane.YES_OPTION);
				}
				else
					loadTable(S, tableSolution);
				break;
			}
			case 1:{
				S = GaussElimination.solveSystemPartivalPivoting(A, B);
				if (S == null){
					JOptionPane.showMessageDialog(frmMatrixOperations, GaussElimination.getMsg(), "Error", JOptionPane.YES_OPTION);
				}
				else
					loadTable(S, tableSolution);

				break;
			}
			case 2:{
				S = GaussElimination.solveSystemPartivalPivotingWithScalar(A, B);
				if (S == null){
					JOptionPane.showMessageDialog(frmMatrixOperations, GaussElimination.getMsg(), "Error", JOptionPane.YES_OPTION);	
				}
				else
					loadTable(S, tableSolution);

				break;
			}
			case 3:{
				Matrix[] LU = DecompositionLU.factorizing(A);
				if (LU == null)
					JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLU.getMsg(), "Error", JOptionPane.YES_OPTION);
				else{
					S = DecompositionLU.solveSysem(LU, B);
				
					if (S == null)
						JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLU.getMsg(), "Error", JOptionPane.YES_OPTION);
					else
						loadTable(S, tableSolution);
				}
				break;
			}
			case 4:{
				Matrix[] LUP = DecompositionLUP.factorizing(A);
				if (LUP == null)
					JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLUP.getMsg(), "Error", JOptionPane.YES_OPTION);
				else{
					S = DecompositionLUP.solveSytsem(LUP, B);
					if (S == null)
						JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLUP.getMsg(), "Error", JOptionPane.YES_OPTION);
					else
						loadTable(S, tableSolution);
				}

				break;
			}
			case 5:{
				Matrix[] LDLT = DecompositionLDLT.factorizing(A);
				if (LDLT == null)
					JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLDLT.getMsg(), "Error", JOptionPane.YES_OPTION);
				else{				
					S = DecompositionLDLT.solveSysem(LDLT, B);
					if (S == null)
						JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLDLT.getMsg(), "Error", JOptionPane.YES_OPTION);
					else
						loadTable(S, tableSolution);
				}
				
				break;
			}
			case 6:{
				Matrix L = Cholesky.factorizing(A);
				if (L == null)
					JOptionPane.showMessageDialog(frmMatrixOperations, Cholesky.getMsg(), "Error", JOptionPane.YES_OPTION);
				else{
					S = Cholesky.solveSysem(L, B);
					if (S == null)
						JOptionPane.showMessageDialog(frmMatrixOperations, Cholesky.getMsg(), "Error", JOptionPane.YES_OPTION);
					else
						loadTable(S, tableSolution);
				}
				break;
			}
			case 7:{
				Matrix[] LU = Crout.factorizing(A);
				if (LU == null)
					JOptionPane.showMessageDialog(frmMatrixOperations, Crout.getMsg(), "Error", JOptionPane.YES_OPTION);
				else{
					S = Crout.solveSysem(LU, B);
					if (S == null)
						JOptionPane.showMessageDialog(frmMatrixOperations, Crout.getMsg(), "Error", JOptionPane.YES_OPTION);
					else
						loadTable(S, tableSolution);
				}
				
				break;
			}
		}
	}
	
	private void decomposite(){
		switch (comboBox_1.getSelectedIndex()){
		
		case 0:{
			M = tableToMatrix(tableM);
			Matrix[] LU = DecompositionLU.factorizing(M);
			
			removeAllElementsInTablesDecomposition();
			
			if (LU == null)
				JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLU.getMsg(), "Error", JOptionPane.YES_OPTION);
			else{
				
				loadTable(LU[0], tableF1);
				loadTable(LU[1], tableF2);
			}

			break;
		}
		case 1:{
			M = tableToMatrix(tableM);
			Matrix[] LUP = DecompositionLUP.factorizing(M);
			
			removeAllElementsInTablesDecomposition();
			
			if (LUP == null)
				JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLUP.getMsg(), "Error", JOptionPane.YES_OPTION);			
			else{
				loadTable(LUP[0], tableF1);
				loadTable(LUP[1], tableF2);
				loadTable(LUP[2], tableF3);
			}
			
			break;
		}
		case 2:{
			M = tableToMatrix(tableM);
			Matrix[] LDLT = DecompositionLDLT.factorizing(M);
			
			removeAllElementsInTablesDecomposition();
			
			if (LDLT == null)
				JOptionPane.showMessageDialog(frmMatrixOperations, DecompositionLDLT.getMsg(), "Error", JOptionPane.YES_OPTION);
			else{
				loadTable(LDLT[0], tableF1);
				loadTable(LDLT[1], tableF2);
				loadTable(LDLT[0].transposedMatrix(), tableF3);
			}
			
			break;
		}
		case 3:{
			M = tableToMatrix(tableM);
			Matrix L = Cholesky.factorizing(M);
			
			removeAllElementsInTablesDecomposition();
			
			if (L == null)
				JOptionPane.showMessageDialog(frmMatrixOperations, Cholesky.getMsg(), "Error", JOptionPane.YES_OPTION);
			else{
				loadTable(L, tableF1);
				loadTable(L.transposedMatrix(), tableF2);
				loadTable(new Matrix(0, 0), tableF3);
			}
			
			break;
		}
		case 4:{
			M = tableToMatrix(tableM);
			Matrix[] LU = Crout.factorizing(M);
			
			removeAllElementsInTablesDecomposition();
			
			if (LU == null)
				JOptionPane.showMessageDialog(frmMatrixOperations, Crout.getMsg(), "Error", JOptionPane.YES_OPTION);
			else{
				loadTable(LU[0], tableF1);
				loadTable(LU[1], tableF2);
				loadTable(new Matrix(0, 0), tableF3);
			}
			
			break;
		}
	
	}

	}
	
	private void loadTable(Matrix M, JTable table){
		
		Double[][] matrix = new Double[M.getN()][M.getM()];
				
		for (int j = 0; j < M.getN(); j++) {
			for (int i = 0; i < M.getM(); i++) {
				
				matrix[j][i] = M.getValue(j, i);
						
			}
		}

		
		((DefaultTableModel)table.getModel()).getDataVector().removeAllElements();
		((DefaultTableModel)table.getModel()).setRowCount(0);		
		
		table.setModel(new DefaultTableModel(matrix, new String[M.getM()]){

			Class[] columnTypes = new Class[]{
				Double.class
			};
			
			public Class getColumnClass(int columnIndex) {
				return columnTypes[0];
			}
		});

		
		((DefaultTableModel)table.getModel()).setColumnCount(M.getM());
		((DefaultTableModel)table.getModel()).setRowCount(M.getN());
	}
		
	
	private void LoadFile(Matrix M, JTable table){
		String caminho;
		int retorno;
		
		jFChosen.setDialogTitle("Open file");		
		retorno = jFChosen.showOpenDialog(null);

		if (retorno == JFileChooser.APPROVE_OPTION){
			caminho = jFChosen.getSelectedFile().getAbsolutePath();
			
			try {
				M = FileManager.readMatrix(caminho);
				loadTable(M, table);
				
			} catch (IOException e) {
				
				JOptionPane.showMessageDialog(frmMatrixOperations, "Error load file!", "Error", JOptionPane.YES_OPTION);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}

	private void LoadFileCoefficientsTerms(){
		LoadFile(this.A, tableCoefficients);
	}
		
	private void LoadFileIndependentsTerms(){
		LoadFile(this.B, tableIndendents);
	}
	
	private void LoadFileM(){
		LoadFile(this.M, tableM);
	}
	
	private Matrix tableToMatrix(JTable table){
		Matrix matrix = new Matrix(table.getRowCount(), table.getColumnCount());
		
		for (int i = 0; i < matrix.getN(); i++) {
			for (int j = 0; j < matrix.getM(); j++) {
				matrix.setValue(i, j, ((double)(Double)table.getValueAt(i, j)));
			}
		}
		
		return matrix;
	}
	
	private void SaveFile(Matrix M){
		String caminho;
		int retorno;
		
		jFChosen.setDialogTitle("Save file");		
		retorno = jFChosen.showSaveDialog(null);

		if (retorno == JFileChooser.APPROVE_OPTION){
			caminho = jFChosen.getSelectedFile().getAbsolutePath();
			
			try {
				FileManager.write(M, caminho);
				JOptionPane.showMessageDialog(frmMatrixOperations, "Files saved with sucess!", "Saved", JOptionPane.INFORMATION_MESSAGE);
				
			} catch (IOException e) {
				JOptionPane.showMessageDialog(frmMatrixOperations, "Error save file!", "Error", JOptionPane.YES_OPTION);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}
	

	private void SaveFileCoefficientsTerms(){
		Matrix A = tableToMatrix(tableCoefficients);
		SaveFile(A);
	}
	
	private void SaveFileIndependentsTerms(){
		Matrix B = tableToMatrix(tableIndendents);
		SaveFile(B);
	}
	
	private void SaveFileSolution(){
		Matrix S = tableToMatrix(tableSolution);
		SaveFile(S);
	}
	
	private void SaveFileM(){
		Matrix M = tableToMatrix(tableM);
		SaveFile(M);
	}

	
	private void SaveFileF1(){
		Matrix F1 = tableToMatrix(tableF1);
		SaveFile(F1);
	}
	
	private void SaveFileF2(){
		Matrix F2 = tableToMatrix(tableF2);
		SaveFile(F2);
	}
	
	private void SaveFileF3(){
		Matrix F3 = tableToMatrix(tableF3);
		SaveFile(F3);
	}
	
	private void removeAllElementsInTablesDecomposition(){
		
		int rows = ((DefaultTableModel)tableF1.getModel()).getRowCount(); 
		for(int i = rows - 1; i >=0; i--)
		{
			((DefaultTableModel)tableF1.getModel()).removeRow(i);
		}
		
		rows = ((DefaultTableModel)tableF2.getModel()).getRowCount(); 
		for(int i = rows - 1; i >=0; i--)
		{
			((DefaultTableModel)tableF2.getModel()).removeRow(i);
		}
		
		rows = ((DefaultTableModel)tableF3.getModel()).getRowCount(); 
		for(int i = rows - 1; i >=0; i--)
		{
			((DefaultTableModel)tableF3.getModel()).removeRow(i);
		}

	}


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Janela window = new Janela();
					window.frmMatrixOperations.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void updateLabels(){
		

		switch (comboBox_1.getSelectedIndex()){
		
			case 0:{
				
				((TitledBorder) panel_5.getBorder()).setTitle("L");
				((TitledBorder) panel_6.getBorder()).setTitle("U");
				((TitledBorder) panel_7.getBorder()).setTitle("Matrix");
				
				panel_5.repaint();
				panel_6.repaint();
				panel_7.repaint();
				
				panel_7.setEnabled(false);
				tableF3.setEnabled(false);;
				buttonSaveO.setEnabled(false);
				break;
			}
			case 1:{
				
				((TitledBorder) panel_5.getBorder()).setTitle("L");
				((TitledBorder) panel_6.getBorder()).setTitle("U");
				((TitledBorder) panel_7.getBorder()).setTitle("P");
				
				panel_5.repaint();
				panel_6.repaint();
				panel_7.repaint();
				
				panel_7.setEnabled(true);
				tableF3.setEnabled(true);;
				buttonSaveO.setEnabled(true);

				break;
			}
			case 2:{
				
				((TitledBorder) panel_5.getBorder()).setTitle("L");
				((TitledBorder) panel_6.getBorder()).setTitle("D");
				((TitledBorder) panel_7.getBorder()).setTitle("L^T");
				
				panel_5.repaint();
				panel_6.repaint();
				panel_7.repaint();
				
				panel_7.setEnabled(true);
				tableF3.setEnabled(true);;
				buttonSaveO.setEnabled(true);
				break;
			}
			case 3:{
				
				((TitledBorder) panel_5.getBorder()).setTitle("L");
				((TitledBorder) panel_6.getBorder()).setTitle("L^T");
				((TitledBorder) panel_7.getBorder()).setTitle("Matrix");
				
				panel_5.repaint();
				panel_6.repaint();
				panel_7.repaint();
				
				panel_7.setEnabled(false);
				tableF3.setEnabled(false);;
				buttonSaveO.setEnabled(false);
				break;
			}
			case 4:{
				
				((TitledBorder) panel_5.getBorder()).setTitle("L");
				((TitledBorder) panel_6.getBorder()).setTitle("U");
				((TitledBorder) panel_7.getBorder()).setTitle("Matrix");
				
				panel_5.repaint();
				panel_6.repaint();
				panel_7.repaint();
				
				panel_7.setEnabled(false);
				tableF3.setEnabled(false);;
				buttonSaveO.setEnabled(false);
				break;
			}
		
		}
	}

	/**
	 * Create the application.
	 */
	public Janela() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMatrixOperations = new JFrame();
		frmMatrixOperations.setTitle("Matrix Operations");
		frmMatrixOperations.setResizable(false);
		frmMatrixOperations.setBounds(100, 100, 714, 611);
		frmMatrixOperations.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		GroupLayout groupLayout = new GroupLayout(frmMatrixOperations.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 685, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(28, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(tabbedPane)
					.addContainerGap())
		);
		
		panel = new JPanel();
		tabbedPane.addTab("System solver", null, panel, null);
				
		panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix of independents Terms", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonLoadI = new JButton("Load");
		buttonLoadI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadFileIndependentsTerms();
			}
		});
		buttonLoadI.setMnemonic('r');
		
		buttonSaveI = new JButton("Save");
		buttonSaveI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SaveFileIndependentsTerms();
			}
		});
		buttonSaveI.setMnemonic('s');
	
		
		scrollPane = new JScrollPane();
		
		JButton button_1 = new JButton("New");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n;
				int m;
				String value;
				
				value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of rows n:", "New", JOptionPane.OK_CANCEL_OPTION);
				
				if (value != null){
					n = Integer.valueOf(value);
					
					value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of columns m:", "New", JOptionPane.OK_CANCEL_OPTION);
					
					if (value != null){
						m = Integer.valueOf(value);
						
						B = Matrix.getNullMatrix(n, m);
												
						loadTable(B, tableIndendents);
					}
				}				
			}
		});
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonLoadI)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonSaveI)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(buttonLoadI, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(buttonSaveI)
						.addComponent(button_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableIndendents = new JTable();
		scrollPane.setViewportView(tableIndendents);
		panel_2.setLayout(gl_panel_2);
				
		panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix of Coefficients", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonLoadC = new JButton("Load");
		buttonLoadC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				LoadFileCoefficientsTerms();
			}
		});
		buttonLoadC.setMnemonic('r');
		
		buttonSaveC = new JButton("Save");
		buttonSaveC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SaveFileCoefficientsTerms();
			}
		});
		buttonSaveC.setMnemonic('s');
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		btnNew = new JButton("New");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n;
				int m;
				String value;
				
				value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of rows n:", "New", JOptionPane.OK_CANCEL_OPTION);
				
				if (value != null){
					n = Integer.valueOf(value);
					
					value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of columns m:", "New", JOptionPane.OK_CANCEL_OPTION);
					
					if (value != null){
						m = Integer.valueOf(value);
						
						A = Matrix.getNullMatrix(n, m);
												
						loadTable(A, tableCoefficients);
					}
				}				
			}
		});
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_3.createSequentialGroup()
							.addComponent(btnNew, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonLoadC)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonSaveC)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(buttonLoadC, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(buttonSaveC)
						.addComponent(btnNew))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableCoefficients = new JTable();
		tableCoefficients.setCellSelectionEnabled(true);
		tableCoefficients.setRowSelectionAllowed(false);
		tableCoefficients.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableCoefficients.setColumnSelectionAllowed(false);
		scrollPane_1.setViewportView(tableCoefficients);
		panel_3.setLayout(gl_panel_3);
	
		
		lblNewLabel = new JLabel("Method:");
		
		
		comboBoxSystem = new JComboBox();
		comboBoxSystem.setModel(new DefaultComboBoxModel(new String[] {"Method of Gauss without pivoting", "Method of Gauss with partial pivoting", "Method of Gauss with scalar partial pivoting", "Using factorization LU", "Using factorization LUP", "Using factorization LDLT", "Using Cholesky", "Using Crout"}));
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(null, "Solution", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		btnSolve = new JButton("Solve");
		btnSolve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ((tableCoefficients.getRowCount() > 0) && (tableIndendents.getRowCount() > 0))
					solveSystem();
				else
					JOptionPane.showMessageDialog(frmMatrixOperations, "Error! Load the matrix of system", "Error", JOptionPane.YES_OPTION);
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(gl_panel.createSequentialGroup()
								.addGap(12)
								.addComponent(lblNewLabel)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(comboBoxSystem, GroupLayout.PREFERRED_SIZE, 371, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnSolve, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE))
							.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
								.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(comboBoxSystem, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnSolve))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_4, GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SaveFileSolution();
			}
		});
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane_2, GroupLayout.PREFERRED_SIZE, 508, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnSave, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(scrollPane_2, GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
						.addComponent(btnSave)))
		);
		
		tableSolution = new JTable();
		tableSolution.setCellSelectionEnabled(true);
		tableSolution.setRowSelectionAllowed(false);
		tableSolution.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableSolution.setColumnSelectionAllowed(false);
		scrollPane_2.setViewportView(tableSolution);
		panel_4.setLayout(gl_panel_4);
		panel.setLayout(gl_panel);
		
		
		panel_1 = new JPanel();
		tabbedPane.addTab("Decomposition", null, panel_1, null);
		
		JLabel label = new JLabel("Method:");
		
		comboBox_1 = new JComboBox();
		
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateLabels();
			}
		});
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"LU", "LUP", "LDLT", "Cholesky", "Crout"}));
		
		panel_5 = new JPanel();
		panel_5.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonSaveL = new JButton("Save");
		buttonSaveL.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SaveFileF1();
			}
		});
		buttonSaveL.setMnemonic('s');
		
		scrollPane_3 = new JScrollPane();
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_5.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane_3, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addComponent(buttonSaveL))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_5.setVerticalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addComponent(buttonSaveL)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_3, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableF1 = new JTable();
		tableF1.setCellSelectionEnabled(true);
		tableF1.setRowSelectionAllowed(false);
		tableF1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableF1.setColumnSelectionAllowed(false);
		scrollPane_3.setViewportView(tableF1);
		panel_5.setLayout(gl_panel_5);
		
		
		panel_6 = new JPanel();
		panel_6.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonSaveU = new JButton("Save");
		buttonSaveU.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SaveFileF2();
			}
		});
		buttonSaveU.setMnemonic('s');
		
		scrollPane_4 = new JScrollPane();
		GroupLayout gl_panel_6 = new GroupLayout(panel_6);
		gl_panel_6.setHorizontalGroup(
			gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_6.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_6.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane_4, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addComponent(buttonSaveU))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_6.setVerticalGroup(
			gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_6.createSequentialGroup()
					.addComponent(buttonSaveU)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_4, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableF2 = new JTable();
		tableF2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableF2.setRowSelectionAllowed(false);
		tableF2.setColumnSelectionAllowed(false);
		tableF2.setCellSelectionEnabled(true);
		scrollPane_4.setViewportView(tableF2);
		panel_6.setLayout(gl_panel_6);
		
		
		panel_7 = new JPanel();
		panel_7.setEnabled(false);
		panel_7.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonSaveO = new JButton("Save");
		buttonSaveO.setEnabled(false);
		buttonSaveO.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SaveFileF3();
			}
		});
		buttonSaveO.setMnemonic('s');
		
		JScrollPane scrollPane_5 = new JScrollPane();
		GroupLayout gl_panel_7 = new GroupLayout(panel_7);
		gl_panel_7.setHorizontalGroup(
			gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_7.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane_5, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addComponent(buttonSaveO))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_7.setVerticalGroup(
			gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup()
					.addComponent(buttonSaveO)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_5, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableF3 = new JTable();
		tableF3.setEnabled(false);
		tableF3.setCellSelectionEnabled(true);
		tableF3.setRowSelectionAllowed(false);
		tableF3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableF3.setColumnSelectionAllowed(false);
		scrollPane_5.setViewportView(tableF3);
		panel_7.setLayout(gl_panel_7);
		
		panel_8 = new JPanel();
		panel_8.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Matrix", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		buttonLoadM = new JButton("Load");
		buttonLoadM.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadFileM();
			}
		});
		buttonLoadM.setMnemonic('r');
		
		buttonSaveM = new JButton("Save");
		buttonSaveM.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SaveFileM();
			}
		});
		buttonSaveM.setMnemonic('s');
		
		scrollPane_6 = new JScrollPane();
		
		button = new JButton("New");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n;
				int m;
				String value;
				
				value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of rows n:", "New", JOptionPane.OK_CANCEL_OPTION);
				
				if (value != null){
					n = Integer.valueOf(value);
					
					value = JOptionPane.showInputDialog(frmMatrixOperations, "Enter with the quantity of columns m:", "New", JOptionPane.OK_CANCEL_OPTION);
					
					if (value != null){
						m = Integer.valueOf(value);
						
						M = Matrix.getNullMatrix(n, m);
												
						loadTable(M, tableM);
					}
				}				
			}
		});
		GroupLayout gl_panel_8 = new GroupLayout(panel_8);
		gl_panel_8.setHorizontalGroup(
			gl_panel_8.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_8.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_8.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane_6, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_8.createSequentialGroup()
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonLoadM)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonSaveM)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_8.setVerticalGroup(
			gl_panel_8.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_8.createSequentialGroup()
					.addGroup(gl_panel_8.createParallelGroup(Alignment.BASELINE)
						.addComponent(buttonLoadM, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(buttonSaveM)
						.addComponent(button))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_6, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		
		tableM = new JTable();
		tableM.setCellSelectionEnabled(true);
		tableM.setRowSelectionAllowed(false);
		tableM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableM.setColumnSelectionAllowed(false);
		scrollPane_6.setViewportView(tableM);
		panel_8.setLayout(gl_panel_8);
		
		btnDecompite = new JButton("Decomposite");
		btnDecompite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				decomposite();
			}
		});
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel_8, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
							.addComponent(panel_7, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE)
							.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnDecompite))
					.addContainerGap(18, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGap(15)
									.addComponent(label))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addContainerGap()
									.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addComponent(btnDecompite)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(panel_8, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
								.addComponent(panel_7, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
								.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE))
							.addContainerGap())
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
							.addGap(261))))
		);
		panel_1.setLayout(gl_panel_1);
		frmMatrixOperations.getContentPane().setLayout(groupLayout);
		
		((TitledBorder) panel_5.getBorder()).setTitle("L");
		((TitledBorder) panel_6.getBorder()).setTitle("U");
		
		jFChosen = new JFileChooser();
		jFChosen.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return "Text in ASCII file (.txt)";
			}
			
			@Override
			public boolean accept(File arg0) {
				// TODO Auto-generated method stub
				return arg0.getName().toLowerCase().endsWith("txt") || arg0.isDirectory();
			}
		});

	}
}
